$(document).ready(function(){
	$("#loadWareMoreBtn").click(appendWares);
	$("#loadDetailBtn").click(loadWareDetail);
	$("#searchBtn").click(loadWares);
	// 选择好商品之后，确定按钮
	$("#addWareSureBtn").click(function() {
		toggle();
		// 添加删除事件
		$("#erp-order-detail .erp-ware-list-del").unbind("click").click(delWare);
	});
	$("#submitBtn").click(submitOrder);
});
/**
 * 提交单据
 */
function submitOrder() {
	// 获取和校验基本信息
	var data = {};
	data.title = $("input[name='title']").val();
	if(!data.title || data.title.length < 2) {
		showLayer("标题不能为空或少于2个字");
		return false;
	}
	data.priceTotle = $("input[name='priceTotle']").val();
	if(!data.priceTotle) {
		showLayer("商品总价不能为空");
		return false;
	}
	data.orderTime = $("input[name='orderTime']").val();
	if(!data.orderTime) {
		showLayer("下单时间不能为空");
		return false;
	}
	data.type = $("input[name='o_type']").val();
	data.remark = $("input[name='remark']").val();
	data.wares = [];
	// 遍历商品列表
	$("#erp-order-detail .erp-ware-list").each(function(){
		var ware = {};
		var wid = $(this).attr("wid");
		var price = $("#o_price_" + wid).val();
		var number = $("#o_number_" + wid).val();
		
		data.wares.push({
			wareId: wid,
			wareName: $("#o_title_" + wid).html(),
			priceOriginal: $("#o_price_" + wid).val(),
			number: $("#o_number_" + wid).val(),
			type: $("#o_type_" + wid).val()
		});
	});
	
}

/**
 * 点击删除按钮
 */
function delWare() {
	var wid= $(this).attr("wid");
	$("#ware_" + wid).remove();
	// 从数组移除
	var index = waresExsit.indexOf(wid);
    if (index > -1) {
        waresExsit.splice(index, 1);
    }
    computePrice();
}

/**
 * 加载商品明细，供选择
 */
function loadWareDetail() {
	toggle();
	// 默认加载商品的最新5条明细 TODO GXG
	
//	$.ajax({type:"POST", url: "/ware/save.html",
//		data: {phone: mobile},
//		dataType: "json",
//		beforeSend : function() {
//			// 设置点击事件不可用
//			$("#sendValidateCodeBtn").unbind("click");
//		}, success: function(data) {
//			if(data && data.code == 1) {
//				setTime();
//				$("#sendValidateCodeBtn").removeClass("yellow-color").addClass("gray-color");
//			} else {
//				bindSendVcClick();
//			}
//		}, error : function() {
//			bindSendVcClick();
//		}
//	});
}
/**
 * 加载商品明细数据到页面展示
 */
function loadWares() {
	$("#erp-ware-chooise .erp-ware-list").remove();
	appendWares();
}
/**
 * 加载商品明细数据到页面展示
 */
var waresExsit = [];
function appendWares() {
	var data = [];
	for(var j = 1; j < 6; j++) {
		data.push({
			id: j + 1,
			name: "名称" + j,
			priceOriginal: 10+j,
			numberInventory: 21 + j
		});
	}
	
	var html = [];
	for(var i in data) {
		html.push('<li class="erp-ware-list">');
		html.push('<div class="erp-ware-list-title" id="title_'+data[i].id+'">'+data[i].name+'</div>');
		html.push('<div class="erp-ware-list-price">&yen;<span id="price_'+data[i].id+'">'+data[i].priceOriginal+'</span></div>');
		html.push('<div class="erp-ware-list-number">库存：<span id="number_'+data[i].id+'">'+data[i].numberInventory+'</span></div>');
		html.push('<div class="erp-ware-list-del erp-ware-list-add" wid='+data[i].id+'></div>');
		html.push('</li>');
	}
	$("#loadWareMoreBtn").before(html.join(""));
	// 添加选择事件
	$("#erp-ware-chooise .erp-ware-list-add").unbind("click").click(chooesWare);
}

/**
 * 选择商品后事件
 */
function chooesWare() {
	var wid= $(this).attr("wid");
	// 判断商品是否已经添加过
	
	if(waresExsit.indexOf(wid) > -1) {
		showLayer('商品已经加入过'); 
		return false;
	}
	waresExsit.push(wid);
	var html = [];
	html.push('<li class="erp-ware-list" id="ware_'+wid+'" wid='+wid+'>');
	html.push('<input id="o_type_'+wid+'" type="hidden" value="'+$('input[name="type"]').val()+'">');
	html.push('<div class="erp-ware-list-title" id="o_title_'+wid+'">'+$("#title_" + wid).html()+'</div>');
	html.push('<div class="erp-ware-list-price">&yen;<input id="o_price_'+wid+'" value="'+$("#price_" + wid).html()+'"></div>');
	html.push('<div class="erp-ware-list-number">数量：<input id="o_number_'+wid+'" type="number" value="1"></div>');
	html.push('<div class="erp-ware-list-del" wid='+wid+'></div>');
	html.push('</li>');
	$("#loadDetailBtn").before(html.join(""));
	// 绑定计算事件
	$("#erp-order-detail .erp-ware-list input").unbind("change").change(computePrice);
	computePrice();
}

/**
 * 计算总价,遍历计算
 */
function computePrice() {
	var priceTotal = 0;
	$("#erp-order-detail .erp-ware-list").each(function(){
		var wid = $(this).attr("wid");
		var price = $("#o_price_" + wid).val();
		var number = $("#o_number_" + wid).val();
		
		priceTotal += (price ? parseInt(price) : 0) * (number ? parseInt(number) : 1);
	});
	$("input[name='priceTotle']").val(priceTotal);
}

/**
 * 显示隐藏DIV
 */
function toggle() {
	$("#erp-order-detail").toggle();
	$("#erp-ware-chooise").toggle();
}
/**
 * 显示弹出框
 * @param {Object} content
 */
function showLayer(content) {
	layer.open({
	  title: '提示'
	  ,content: content
	}); 
}
